package com.yanqu.road.server.manger.activity.deepfishing;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.deepfishing.config.*;
import com.yanqu.road.entity.activity.deepfishing.entity.DeepFishingBagItemData;
import com.yanqu.road.entity.activity.deepfishing.entity.DeepFishingBeachCombItemData;
import com.yanqu.road.entity.activity.deepfishing.entity.DeepFishingCalValueAndWeight;
import com.yanqu.road.entity.activity.deepfishing.entity.DeepFishingOcurConfig;
import com.yanqu.road.entity.config.system.PropertyWeightItem;
import com.yanqu.road.entity.enums.activity.deepfishing.eDeepFishRodPartType;
import com.yanqu.road.entity.enums.activity.deepfishing.eDeepFishingEventType;
import com.yanqu.road.entity.enums.activity.deepfishing.eDeepFishingObjectType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.DeepFishingBusiness;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.pb.activity.DeepFishingProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.deepseafishing.DeepFishingModule;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


public class DeepFishingMgr extends TempMgr {

    private static ActivityInfo activityInfo;

    private static DeepFishingConfig config;

    private static Map<String,DeepFishingProto.deepFishingConfigSyncMsg.Builder> configMsgMap;

    private static RandomHelper randomHelper = new RandomHelper();


    @Override
    public boolean init() throws Exception {
        return false;
    }

    @Override
    public boolean reload() throws Exception {
        return super.reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        return super.save();
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static void setActivityInfo(ActivityInfo activityInfo) {
        DeepFishingMgr.activityInfo = activityInfo;
    }

    public static void reloadActivityData(){
        getLogger().info("reload deepFishing activity start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.DeepFishing.getValue());
        if (openActivityInfoList.size() <= 0) {
            getLogger().info("reload deepFishing activity finish, no activity in show time.");
            config = null;
            activityInfo = null;
            return;
        }
        ActivityInfo tmpActivityInfo = openActivityInfoList.get(0);
        //活动配置
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityInfo.getActivityId());
        boolean configRight = checkActivityConfig(activityConfigMap);
        if (!configRight){
            config = null;
            activityInfo = null;
            getLogger().error("深海觅诊活动{}加载Config字段缺失错误,请检查活动表",tmpActivityInfo.getActivityId());
            return;
        }
        activityInfo = tmpActivityInfo;
        Map<Integer, DeepFishingFishConfig> tmpFishConfigMap = DeepFishingBusiness.getDeepFishingFishConfigList(activityInfo.getActivityId());
        Map<Integer, DeepFishingEquipConfig> tmpEquipConfigMap = DeepFishingBusiness.getDeepFishingEquipConfigList(activityInfo.getActivityId());
        Map<Integer, DeepFishingEventConfig> tmpEventConfigMap = DeepFishingBusiness.getDeepFishingEventConfigList(activityInfo.getActivityId());
        Map<Integer,List<DeepFishingBeachCombConfig>> tmpDeepFishingBeachCombConfig = DeepFishingBusiness.getDeepFishingBeachCombConfigList(activityInfo.getActivityId());


        config = new DeepFishingConfig(tmpFishConfigMap,tmpEquipConfigMap,tmpEventConfigMap,tmpDeepFishingBeachCombConfig,activityConfigMap);

        configMsgMap = new ConcurrentHashMap<>();

        //用户重载待定
        // 同步数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            gamePlayer.getModule(DeepFishingModule.class).syncActivity();
        }
        getLogger().info("reload deepFishing activity end");


    }

    public static DeepFishingProto.deepFishingConfigSyncMsg.Builder getConfigMsg(String language){
        if (configMsgMap.get(language) == null) {
            DeepFishingProto.deepFishingConfigSyncMsg.Builder builder = initConfigMsg(config, language);
            configMsgMap.put(language, builder);
        }
        return configMsgMap.get(language);
    }

    /**
     * 初始化某种语言配置同步信息
     */
    public static DeepFishingProto.deepFishingConfigSyncMsg.Builder initConfigMsg(DeepFishingConfig allConfig,String language){
        //总配置
        DeepFishingProto.deepFishingConfigSyncMsg.Builder builder = DeepFishingProto.deepFishingConfigSyncMsg.newBuilder();
        //鱼类配置
        for (DeepFishingFishConfig fishConfig : allConfig.getFishConfigMap().values()){
            DeepFishingProto.deepFishingObjectConfig.Builder fishConfigBuilder = DeepFishingProto.deepFishingObjectConfig.newBuilder();
            fishConfigBuilder.setObjectId(fishConfig.getObjectId());
            fishConfigBuilder.setType(fishConfig.getType());
            fishConfigBuilder.setLittleType(fishConfig.getLittleType());
            fishConfigBuilder.setQuality(fishConfig.getQuality());
            for (DeepFishingOcurConfig ocurConfig:fishConfig.getOcurConfigList()){
                DeepFishingProto.deepFishingOcurConfig.Builder ocurBuilder= DeepFishingProto.deepFishingOcurConfig.newBuilder();
                ocurBuilder.setMinMeter(ocurConfig.ocurMinDeep);
                ocurBuilder.setOcurRate(ocurConfig.ocurRate);
                fishConfigBuilder.addOucrConfig(ocurBuilder);
            }
            fishConfigBuilder.setSpeed(fishConfig.getSpeed());
            for (int num :fishConfig.getCoinRange()){
                fishConfigBuilder.addCoinRange(num);
            }
            fishConfigBuilder.setFollowPro(fishConfig.getFollowPro());
            fishConfigBuilder.setFollowRange(fishConfig.getFollowRange());
            fishConfigBuilder.setFollowSpeed(fishConfig.getFollowSpeed());
            fishConfigBuilder.setName(ServerLanguageMgr.getContent(fishConfig.getName(),language));
            fishConfigBuilder.setScale(fishConfig.getScale());
            for (int weight:fishConfig.getWeightRange()){
                fishConfigBuilder.addWeightRange(weight);
            }
            fishConfigBuilder.setDesc(ServerLanguageMgr.getContent(fishConfig.getDesc(),language)) ;
            fishConfigBuilder.setMoveParam(fishConfig.getMoveParam());
            fishConfigBuilder.setPoolType(fishConfig.getPoolType());
            builder.addObjectConfig(fishConfigBuilder);
        }
        //鱼竿配置
        for (DeepFishingEquipConfig equipConfig:allConfig.getEquipConfigMap().values()){
            DeepFishingProto.deepFishingEquipConfig.Builder equipBuilder = DeepFishingProto.deepFishingEquipConfig.newBuilder();
            equipBuilder.setLvId(equipConfig.getLvId());
            equipBuilder.setType(equipConfig.getType());
            equipBuilder.setLv(equipConfig.getLv());
            equipBuilder.setCost(equipConfig.getCost());
            for (int value:equipConfig.getEffectValue()){
                equipBuilder.addEffectValue(value);
            }
            builder.addEquipConfig(equipBuilder);
        }
        //事件配置
        for (DeepFishingEventConfig eventConfig:allConfig.getEventConfigMap().values()){
            DeepFishingProto.deepFishingEventConfig.Builder eventBuilder = DeepFishingProto.deepFishingEventConfig.newBuilder();
            eventBuilder.setEventId(eventConfig.getEventId());
            eventBuilder.setType(eventConfig.getType());
            eventBuilder.setLittle(eventConfig.getLittle());
            eventBuilder.setWeight(eventConfig.getWeight());
            eventBuilder.setEventValue(eventConfig.getEventValue());
            for (DeepFishingOcurConfig deepFishingOcurConfig:eventConfig.getOcurConfigList()){
                DeepFishingProto.deepFishingOcurConfig.Builder ocurBuilder= DeepFishingProto.deepFishingOcurConfig.newBuilder();
                ocurBuilder.setMinMeter(deepFishingOcurConfig.ocurMinDeep);
                ocurBuilder.setOcurRate(deepFishingOcurConfig.ocurRate);
                eventBuilder.addOcurConfig(ocurBuilder);
            }
            builder.addEventConfig(eventBuilder);
        }

        for (List<DeepFishingBeachCombConfig> beachCombConfigList:allConfig.getBeachCombConfig().values()){
            for (DeepFishingBeachCombConfig beachCombConfig:beachCombConfigList){
                DeepFishingProto.deepFishBeachCombConfig.Builder beachBuilder = DeepFishingProto.deepFishBeachCombConfig.newBuilder();
                beachBuilder.setPointId(beachCombConfig.getPointId());
                beachBuilder.setScene(beachCombConfig.getScene());
                beachBuilder.setPointCode(beachCombConfig.getPointCode());
                beachBuilder.setPos(beachCombConfig.getPos());

                for (PropertyWeightItem weightItem:beachCombConfig.getBigReward()){
                    String string = PropertyHelper.parsePropertyToString(weightItem.getProperty());
                    beachBuilder.addWeight(string);
                }

                for (PropertyWeightItem weightItem:beachCombConfig.getWeight()){
                    String string = PropertyHelper.parsePropertyToString(weightItem.getProperty());
                    beachBuilder.addWeight(string);
                }
                beachBuilder.setType(beachCombConfig.getType());
                beachBuilder.setUrl(beachCombConfig.getUrl());
                builder.addBeachCombConfig(beachBuilder);
            }
        }


        return builder;
    }

    /**
     * 随机赶海的奖励 全随机
     */
    public static Property getRandomBeachCombReward (int pointId){
        Property property = new Property();

        DeepFishingBeachCombConfig config = getBeachCombConfigById(pointId);
        if (config == null){
            return property;
        }
        List<PropertyWeightItem> normal = config.getWeight();
        List<PropertyWeightItem> big = config.getBigReward();
        List<PropertyWeightItem> all = new ArrayList<>();
        all.addAll(normal);
        all.addAll(big);
        property = RandomWeightConfigHelper.getPropertyWeightItem(all);

        return property;
    }

    /**
     * 用id查找赶海配置
     * @param pointId
     * @return
     */

    public static DeepFishingBeachCombConfig getBeachCombConfigById(int pointId){
        Map<Integer, List<DeepFishingBeachCombConfig>> beachCombConfig = config.getBeachCombConfig();
        for (List<DeepFishingBeachCombConfig> configList:beachCombConfig.values()){
            for (DeepFishingBeachCombConfig config:configList){
                if (config.getPointId() == pointId){
                    return config;
                }
            }
        }
        return null;
    }

    /**
     * 随机赶海的大奖！
     */
    public static Property getRandomBeachBigReward(int pointId){
        Property property = new Property();
        DeepFishingBeachCombConfig config = getBeachCombConfigById(pointId);
        if (config == null){
            return property;
        }

        List<PropertyWeightItem> big = config.getBigReward();
        property = RandomWeightConfigHelper.getPropertyWeightItem(big);
        return property;
    }

    /**
     * 检测是否在赶海时间段
     */
    public static boolean inBeachOpenTime(){
        if (config == null){
            return false;
        }
        long now = System.currentTimeMillis();

        long todayZeroTimeStamp = DateHelper.getTodayZeroTimeStamp();
        long startMill = todayZeroTimeStamp + DateHelper.MINUTE_MILLIONS * config.getBeachCombStartTime();
        long endMill = todayZeroTimeStamp + DateHelper.MINUTE_MILLIONS * config.getBeachCombEndTime();
        if (now >= startMill && now <= endMill){
            return true;
        }
        return false;

    }

    /**
     * 检测配置
     */
    public static boolean checkActivityConfig(Map<String, ActivityConfig> configMap){
        List<String> configNameList = getConfigNameList();
        for (String configName : configNameList){
            if (configMap.get(configName) == null){
                getLogger().error("深海觅诊"+configName+"不存在");
                return false;
            }
        }
        return true;
    }

    public static Map<Integer, DeepFishingBeachCombItemData> generateCombItemMap(int beachCombSence,int specialTime){

        Map<Integer, List<DeepFishingBeachCombConfig>> beachCombConfigMap = config.getBeachCombConfig();
        List<DeepFishingBeachCombConfig> itemConfigList = beachCombConfigMap.get(beachCombSence);
        Map<Integer, DeepFishingBeachCombItemData> itemDataMap = new ConcurrentHashMap<>();
        if (itemConfigList == null){
            getLogger().error("活动id{}，深海觅珍活动第{}层配置错误",activityInfo.getActivityId(),beachCombSence);
            return itemDataMap;
        }
        int hasgeneralMustSpecial = 0;
        int maxItemInMap = config.getBeachCombMaxItemInMap();
        if (itemConfigList.size() < maxItemInMap){
            getLogger().error("活动id{}，深海觅珍活动第{}层配置错误,数量不足以生成要求值,数据按照全部生成",activityInfo.getActivityId(),beachCombSence);
            for (DeepFishingBeachCombConfig itemConfig: itemConfigList){
//                if (itemConfig.getBigReward().isEmpty()){
                    DeepFishingBeachCombItemData data = new DeepFishingBeachCombItemData(itemConfig.getPointId(),false,false);
                    itemDataMap.put(itemConfig.getPointId(),data);
//                    continue;
//                }
//                if (hasgeneralMustSpecial >= specialTime){
//                    DeepFishingBeachCombItemData data = new DeepFishingBeachCombItemData(itemConfig.getPointId(),false,false);
//                    itemDataMap.put(itemConfig.getPointId(),data);
//                    continue;
//                }else {
//                    DeepFishingBeachCombItemData data = new DeepFishingBeachCombItemData(itemConfig.getPointId(),false,true);
//                    itemDataMap.put(itemConfig.getPointId(),data);
//                    hasgeneralMustSpecial++;
//                    continue;
//                }
            }
            return itemDataMap;
        }

        List<Integer> differentRandomNum = randomHelper.getRandomDistinctInteger(0,itemConfigList.size()-1, maxItemInMap);

        for (int num:differentRandomNum){
            DeepFishingBeachCombConfig beachCombConfig = itemConfigList.get(num);
            if (beachCombConfig != null){
//                if (beachCombConfig.getBigReward().isEmpty()){
                    DeepFishingBeachCombItemData data = new DeepFishingBeachCombItemData(beachCombConfig.getPointId(),false,false);
                    itemDataMap.put(beachCombConfig.getPointId(),data);
//                    continue;
//                }
//                if (hasgeneralMustSpecial >= specialTime){
//                    DeepFishingBeachCombItemData data = new DeepFishingBeachCombItemData(beachCombConfig.getPointId(),false,false);
//                    itemDataMap.put(beachCombConfig.getPointId(),data);
//                    continue;
//                }else {
//                    DeepFishingBeachCombItemData data = new DeepFishingBeachCombItemData(beachCombConfig.getPointId(),false,true);
//                    itemDataMap.put(beachCombConfig.getPointId(),data);
//                    hasgeneralMustSpecial++;
//                    continue;
//                }
            }
        }
        return itemDataMap;

    }

    /**
     * 所有config字段
     */
    public static List<String> getConfigNameList(){
        List<String> configList = new ArrayList<>();
        configList.add("EXCHANGE_SCORE_ITEM_ID");
        configList.add("SEAFISHING_GANHAI_ITEM_ID");
        configList.add("SEAFISHING_ENERGY_SCORE_NUM");
        configList.add("SEAFISHING_ENERGY_ITEM_ID");
        configList.add("SEAFISHING_ENERGY_MAX_NUM");
        configList.add("SEAFISHING_ENERGY_RECOVERY_NUM");
        configList.add("SEAFISHING_ENERGY_RECOVERY_TIME");
        configList.add("SEAFISHING_GANHAI_DAY_NUM");
        configList.add("SEAFISHING_FISHING_SPEED_DOWN");
        configList.add("SEAFISHING_FISHING_SPEED_BEGIN");
        configList.add("SEAFISHING_FISHING_SPEED_UP_CUT");
        configList.add("SEAFISHING_FISHING_MAP_OBJECT_DIAGONAL");
        configList.add("SEAFISHING_FISHING_MAP_LONG");
        configList.add("SEAFISHING_FISHING_MAP_OBJECT_NUM");
        configList.add("SEAFISHING_FISHING_MAP_OBJECT_SPACE");
        configList.add("SEAFISHING_GANHAI_OPEN_TIME");
        configList.add("SEAFISHING_GANHAI_FLASH_CD");
        configList.add("SEAFISHING_GANHAI_OBJ_MAX_NUM");
        configList.add("SEAFISHING_GANHAI_HIGH_LIMIT");

        configList.add("SHOW_BEAUTY");
        configList.add("SHOW_SKIN_BEAUTY");
        return configList;
    }


    /**
     * 获取赶海体力上限
     * @return
     */
    public static int getMaxBeachCombPower(){
        return config.getDayFreeBeachCombTime();
    }


    /**
     * 获取钓鱼最大的体力
     * @return
     */
    public static int getMaxFishingPower(){
        return config.getFishingPowerMax();
    }

    /**
     * 获取钓鱼体力每次恢复数量
     */
    public static int getMaxFishingRecoveryPowerPerTime(){
        return config.getFishingPowerPerTime();
    }
    /**
     * 钓鱼体力恢复时间间隔（秒）
     */
    public static int getMaxFishingRecoveryPowerCd(){
        return config.getFishPowerRecoveryTime();
    }


    /**
     *  获取钓鱼需要的体力
     */
    public static int getFishingNeedPower(int rodLevelid){
        DeepFishingEquipConfig equipConfig = config.getEquipConfigMap().get(rodLevelid);
        if (equipConfig == null){
            return 1;
        }
        return equipConfig.getEffectValue().get(3);
    }

    /**
     * 获取鱼 物品 配置
     */
    public static DeepFishingFishConfig getObjectConfig(int objectId){
        if (config == null){
            return null;
        }
        if (!config.getFishConfigMap().containsKey(objectId)){
            return null;
        }
        return config.getFishConfigMap().get(objectId);

    }


    /**
     * 获取赶海需要的体力
     */
    public static int getBeachCombNeedPower(){

        return 1;
    }

    /**
     * 获取下一级鱼竿
     * @param oldEquipLevelId
     * @return
     */

    public static DeepFishingEquipConfig getNextLevelEquipConfig(int oldEquipLevelId){
        Map<Integer, DeepFishingEquipConfig> equipConfigMap = config.getEquipConfigMap();
        if (equipConfigMap == null){
            return null;
        }
        DeepFishingEquipConfig oldEquipConfig = equipConfigMap.get(oldEquipLevelId);
        if (oldEquipConfig == null){
            return null;
        }
        for (DeepFishingEquipConfig config:equipConfigMap.values()){
            if (config.getType() == oldEquipConfig.getType() && config.getLv() == oldEquipConfig.getLv()+1){
                return config;
            }
        }
        return null;

    }

    public static DeepFishingConfig getConfig() {
        return config;
    }

    public static int getRandomSpecialTime(){
        if (config == null){
            return 0;
        }
        int min = config.getMinBeachCombHighQualityNum();
        int max = config.getMaxBeachCombHighQualityNum();
        return randomHelper.next(min,max+1);
    }

    public static int checkResultRight(List<DeepFishingBagItemData> bagItemDataList,int deep,long userId,Map<Integer,Integer> eventMap,int rodLevelId){
        DeepFishingEquipConfig deepFishingEquipConfig = DeepFishingMgr.getDeepFishingEquipConfig(rodLevelId);
        if (deepFishingEquipConfig == null){
            getLogger().info("深海觅诊活动id{},缺少鱼竿{}配置,玩家{}结算错误",DeepFishingMgr.getActivityInfo().getActivityId(),rodLevelId,userId);
            return GameErrorCode.E_DEEP_FISHING_RESULT_FISH_CONFIG_ERROR;
        }
        int maxSpecialFish = deepFishingEquipConfig.getEffectValue().get(6);
        int maxSpecialItem = deepFishingEquipConfig.getEffectValue().get(7);
        int resultSpecialFish = 0;
        int resultSpecialItem = 0;

        for (DeepFishingBagItemData fishingBagItemData:bagItemDataList){
            int maxOrderLimit = config.getMapMaxDeep();
            DeepFishingFishConfig objectConfig = getObjectConfig(fishingBagItemData.getObjectId());
            //没配置了都
            if (objectConfig == null){
                getLogger().info("深海觅诊活动id{},缺少鱼类{}配置,玩家{}结算错误",DeepFishingMgr.getActivityInfo().getActivityId(),fishingBagItemData.getObjectId(),userId);
                return GameErrorCode.E_DEEP_FISHING_RESULT_FISH_CONFIG_ERROR;
            }
            //掉出事件或者赶海的了
            if(objectConfig.getType() == eDeepFishingObjectType.beachCombItem.getValue()||objectConfig.getType() == eDeepFishingObjectType.event.getValue()){
                getLogger().info("深海觅诊活动id{},钓上赶海或事件物品{},玩家{}结算错误",DeepFishingMgr.getActivityInfo().getActivityId(),fishingBagItemData.getObjectId(),userId);
                return GameErrorCode.E_DEEP_FISHING_RESULT_NO_FISHING_TYPE;
            }
            if (objectConfig.getType()==eDeepFishingObjectType.fish.getValue()){
                if (objectConfig.getQuality() >= 5){        //无双鱼策划说写死
                    resultSpecialFish++;
                }
            }else if (objectConfig.getType()==eDeepFishingObjectType.item.getValue()){
                if (objectConfig.getLittleType() == 1){
                    resultSpecialItem++;
                }
            }
            if (resultSpecialFish > maxSpecialFish || resultSpecialItem > maxSpecialItem){
                return GameErrorCode.E_DEEP_FISHING_FISHING_DATA_ERROR;
            }
            for (Map.Entry<Integer,Integer> event:eventMap.entrySet()){
                DeepFishingEventConfig eventById = getEventById(event.getKey());
                if (eventById.getType() == eDeepFishingEventType.cycloneEvent.getValue()){
                    maxOrderLimit = event.getValue();
                }
            }

            if (fishingBagItemData.getOrderId() > maxOrderLimit){
                getLogger().info("深海觅诊活动id{},飓风后仍有鱼类,玩家{}结算错误",DeepFishingMgr.getActivityInfo().getActivityId(),userId);
                return GameErrorCode.E_DEEP_FISHING_RESULT_VORTEX_ERROR;
            }
            //鱼比地图快
            if (fishingBagItemData.getMeter() > deep){
                getLogger().info("深海觅诊活动id{},玩家钓上物品{}米数{}大于最后深度{},玩家{}结算错误",DeepFishingMgr.getActivityInfo().getActivityId(),fishingBagItemData.getObjectId(),fishingBagItemData.getMeter(),deep,userId);
                return GameErrorCode.E_DEEP_FISHING_FISH_FAST_THAN_MAP;
            }
            //检测鱼类是否可能出现在这个米数
            if (!fishExistThisDeep(fishingBagItemData,objectConfig)){
                getLogger().info("深海觅诊活动id{},钓上赶海物品{}不应存在于深度{}m,玩家{}结算错误",DeepFishingMgr.getActivityInfo().getActivityId(),fishingBagItemData.getObjectId(),fishingBagItemData.getMeter(),userId);
                return GameErrorCode.E_DEEP_FISHING_FISH_NO_EXIST_THIS_METER;
            }

        }

        return 0;
    }

    public static int getBagMaxCapacity(Map<Integer, Integer> rodPartMap){
        int rodLevelId = rodPartMap.get(eDeepFishRodPartType.fishRod.getValue());
        DeepFishingEquipConfig rodEquipConfig = config.getEquipConfigMap().get(rodLevelId);
        int capacity = rodEquipConfig.getEffectValue().get(0);
        return capacity;
    }

    /**
     * 配置了加成结果也不能校验的深度
     * @param fishingBagItemData
     * @param objectConfig
     * @return
     */

    private static boolean fishExistThisDeep(DeepFishingBagItemData fishingBagItemData,DeepFishingFishConfig objectConfig){
        List<DeepFishingOcurConfig> ocurConfigList = objectConfig.getOcurConfigList();
        int ocurRate = 0;

        for (int i = 0;i < ocurConfigList.size();i++){
            DeepFishingOcurConfig maxDeepConfig = null;
            DeepFishingOcurConfig minDeepConfig = ocurConfigList.get(i);
            if (i+1 < ocurConfigList.size()){
                maxDeepConfig = ocurConfigList.get(i+1);
            }
            if (maxDeepConfig != null){
                if (fishingBagItemData.getMeter() >= minDeepConfig.getOcurMinDeep() && fishingBagItemData.getMeter() < maxDeepConfig.getOcurMinDeep()){
                    ocurRate = minDeepConfig.getOcurRate();
                    break;
                }
            }else {
                ocurRate = minDeepConfig.getOcurRate();
                break;
            }
        }
        if (ocurRate <= 0){
            getLogger().error("深海觅诊活动玩家传入不可能出现的深度");
            return false;
        }
        return true;
    }

    /**
     * 获取随机价值
     * @param objectId
     * @return
     */

    public static DeepFishingCalValueAndWeight getRandomValue(int objectId){
        if (config == null){
            getLogger().error("");
            return new DeepFishingCalValueAndWeight(0,0);
        }
        Map<Integer, DeepFishingFishConfig> fishConfigMap = config.getFishConfigMap();
        DeepFishingFishConfig fishConfig = fishConfigMap.get(objectId);
        if (fishConfig == null){
            getLogger().error("");
            return new DeepFishingCalValueAndWeight(0,0);
        }
        List<Integer> coinRange = fishConfig.getCoinRange();
        int value = randomHelper.next(coinRange.get(0), coinRange.get(1) + 1);
        List<Integer> weightRange = fishConfig.getWeightRange();
        int weight = 0;
        //如果是固定金币
        if ((coinRange.get(1) - coinRange.get(0))==0){
            Random random = new Random();
            //重量最大最小差距
            int weightDiff = weightRange.get(1)-weightRange.get(0);
            if (weightDiff == 0){
                weight = weightRange.get(0);
            }else {
                //全随机
                weight = randomHelper.next(weightRange.get(0), weightRange.get(1) + 1);
            }
        }else {
            BigInteger weightBigInteger = BigInteger.valueOf(weightRange.get(0)).add(BigInteger.valueOf(weightRange.get(1)-weightRange.get(0)).multiply(BigInteger.valueOf((value-coinRange.get(0)))).divide(BigInteger.valueOf(coinRange.get(1)-coinRange.get(0))));
            weight = weightBigInteger.intValue();
        }

        return new DeepFishingCalValueAndWeight(value,weight);
    }

    /**
     * 随机事件
     * @return
     */

    public static List<Integer> randomEvent(){
        if (config == null){
            return new ArrayList<>();
        }
        List<Integer> resultList = new ArrayList<>();

        Map<Integer,List<Integer>> weightMap = new ConcurrentHashMap<>();
        Map<Integer,List<Integer>> eventIdMapByIndex = new ConcurrentHashMap<>();

        Map<Integer, DeepFishingEventConfig> eventConfigMap = config.getEventConfigMap();
        for (DeepFishingEventConfig eventConfig : eventConfigMap.values()){
            weightMap.computeIfAbsent(eventConfig.getType(), k -> new ArrayList<>());
            eventIdMapByIndex.computeIfAbsent(eventConfig.getType(), k -> new ArrayList<>());
            weightMap.get(eventConfig.getType()).add(eventConfig.getWeight());
            eventIdMapByIndex.get(eventConfig.getType()).add(eventConfig.getEventId());
        }

        for (Map.Entry<Integer,List<Integer>> entry:weightMap.entrySet()){
            int randomIndex = randomHelper.getSafeRandomIndexByWeight(entry.getValue());
            Integer eventId = eventIdMapByIndex.get(entry.getKey()).get(randomIndex);
            resultList.add(eventId);
        }
        return resultList;
    }

    /**
     * 获取buff增加的值
     * @param buffType
     * @return
     */

    public static int getBuffAddRate(int buffType) {
        if (config == null) {
            return 0;
        }
        int buffIdByType = getBuffIdByType(buffType);
        Map<Integer, DeepFishingEventConfig> eventConfigMap = config.getEventConfigMap();
        DeepFishingEventConfig eventConfig = eventConfigMap.get(buffIdByType);
        List<Integer> list = StringUtils.stringToIntegerList(eventConfig.getEventValue(), "\\|");
        return list.get(0);

    }

    /**
     * 用小类型找buffid
     * @param smallType
     * @return
     */

    public static int getBuffIdByType(int smallType){
        if (config == null){
            return 0;
        }
        Map<Integer, DeepFishingEventConfig> eventConfigMap = config.getEventConfigMap();
        for (DeepFishingEventConfig eventConfig:eventConfigMap.values()){
            if (eventConfig.getType() == eDeepFishingEventType.buffEvent.getValue() && eventConfig.getLittle() == smallType){
                return eventConfig.getEventId();
            }
        }
        return 0;
    }

    /**
     * 用type获取buff
     * @param smallType
     * @return
     */
    public static DeepFishingEventConfig getBuffConfigByType(int smallType){
        if (config == null){
            return null;
        }
        Map<Integer, DeepFishingEventConfig> eventConfigMap = config.getEventConfigMap();
        for (DeepFishingEventConfig eventConfig:eventConfigMap.values()){
            if (eventConfig.getType() == 2 && eventConfig.getLittle() == smallType){
                return eventConfig;
            }
        }
        return null;
    }

    private static DeepFishingEventConfig getEventById (int eventId){
        if (config == null){
            return null;
        }
        return config.getEventConfigMap().get(eventId);
    }


    /**
     * 获取深海觅诊鱼竿配置
     * @param equipId
     * @return
     */

    public static DeepFishingEquipConfig getDeepFishingEquipConfig(int equipId){
        if (config == null){
            return null;
        }
        return config.getEquipConfigMap().get(equipId);
    }

    /**
     * 获取鱼竿的金币加成
     * @param rodPartMap
     * @return
     */

    public static int getRodCoinAdd(Map<Integer, Integer> rodPartMap){
        int addition = 1000;

//        //先找鱼护的加成
//        int protectionLevelId = rodPartMap.get(eDeepFishRodPartType.fishRod.getValue());
//        DeepFishingEquipConfig equipConfig = config.getEquipConfigMap().get(protectionLevelId);
//        if (equipConfig ==null){
//            return addition;
//        }
//        addition += equipConfig.getEffectValue().get(0);

        //找鱼竿的加成
        int rodLevelId = rodPartMap.get(eDeepFishRodPartType.fishRod.getValue());
        DeepFishingEquipConfig rodEquipConfig = config.getEquipConfigMap().get(rodLevelId);
        if (rodEquipConfig ==null){
            return addition;
        }
        int totalAddition = rodEquipConfig.getEffectValue().get(1)+addition;
        return totalAddition;
    }



//    public static int getRodScoreAdd(Map<Integer, Integer> rodPartMap){
//        int addition = 1000;
//        int rodLevelId = rodPartMap.get(eDeepFishRodPartType.fishRod.getValue());
//        DeepFishingEquipConfig rodEquipConfig = config.getEquipConfigMap().get(rodLevelId);
//        if (rodEquipConfig == null){
//            return addition;
//        }
//        addition = addition * rodEquipConfig.getEffectValue().get(3);
//        return addition;
//    }


    //运营测试工具
//    public static void runTest() throws IOException {
//        File file =new File("test_fish.txt");
//        if(!file.exists()){
//            file.createNewFile();
//        }
//        System.out.println(file.getAbsolutePath());
//        FileWriter fileWritter = new FileWriter(file.getName(),true);
//        for (int i = 0;i < 100000;i++){
//            int nowLevel = 1;
//            int hasSpecialTime = 0;
//            int bigTime = 0;
//            long totalScore = 0;
//            int randomSpecialTime = getRandomSpecialTime();
//            Map<Integer, DeepFishingBeachCombItemData> itemDataMap = generateCombItemMap(nowLevel, randomSpecialTime);
//
//            for (int time = 0;time<150;time++){
//                Collection<DeepFishingBeachCombItemData> values = itemDataMap.values();
//                List<DeepFishingBeachCombItemData> list = new ArrayList<>(values);
//                Collections.shuffle(list);
//                int scoreReward = 0;
//                for (DeepFishingBeachCombItemData data: list){
//                    if (!data.isItemOpen()){
//                        data.setItemOpen(true);
//                        Property property =new Property();
//                        if (data.isSpecial()){
//                            property = DeepFishingMgr.getRandomBeachBigReward(data.getPointId());
//                        } else {
//                            property = DeepFishingMgr.getRandomBeachCombReward(data.getPointId());
//                        }
//
//                        //判断是不是大奖
//                        boolean isBigReward = false;
//
//                        for (int goodId:property.getGoods().keySet()){
//                            DeepFishingFishConfig fishConfig = config.getFishConfigMap().get(goodId);
//                            int quality = fishConfig.getQuality();
//                            scoreReward = fishConfig.getCoinRange().get(0);
//                            if (quality >=config.getHighQualityGoodThan()){
//                                isBigReward = true;
//                                break;
//                            }
//                        }
//                        if (isBigReward){
//                            bigTime++;
//                            hasSpecialTime++;
//                        }
//                        totalScore+=scoreReward;
//                        break;
//                    }
//                }
//                if (hasSpecialTime >= randomSpecialTime){
//                    Set<Integer> set = DeepFishingMgr.getConfig().getBeachCombConfig().keySet();
//                    if (set.contains(nowLevel+1)){
//                        nowLevel = nowLevel+1;
//                    }else {
//                        nowLevel = 1;
//                    }
//                    hasSpecialTime = 0;
//                    randomSpecialTime = getRandomSpecialTime();
//                    itemDataMap = generateCombItemMap(nowLevel, randomSpecialTime);
//                }
//
//            }
//            System.out.println("玩家 "+i+"积分为: "+totalScore +"大奖次数: "+ bigTime);
//            fileWritter.write("玩家 "+i+"积分为: "+totalScore +"大奖次数: "+ bigTime+"\n");
//
//        }
//        fileWritter.close();
//        System.out.println("finish");
//    }





}
